package com.example.isoftservice.service.impl;

import com.example.isoftservice.mapper.OrderMapper;
import com.example.isoftservice.pojos.Order;
import com.example.isoftservice.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;


    @Override
    @Cacheable(cacheNames ="order",key="#order")
    public Map<String, Object> orderSelect(Order order) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(order);
        // PageHel.startPage(stock.getPage(),stock.getRows());
        List<Map<String, Object>> list = orderMapper.orderSelect(order);
        List<Map<String, Object>> count = orderMapper.orderSelectNumber(order);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        PageInfo<Map<String, Object>> pageInfoNumber = new PageInfo<>(count);
        map.put("count", pageInfoNumber.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    @Override
    public Map<String, Object> selectProduct() {
        Map<String, Object> map = new HashMap<>();

        List<Map<String, Object>> list = orderMapper.selectProduct();
        map.put("data", list);
        return map;
    }


    @Override
    public Map<String, Object> selectUser() {
        Map<String, Object> map = new HashMap<>();

        List<Map<String, Object>> list = orderMapper.selectUser();
        map.put("data", list);
        return map;
    }

    @Override
    public Map<String, Object> selectStoreId() {
        Map<String, Object> map = new HashMap<>();

        List<Map<String, Object>> list = orderMapper.selectStoreId();
        map.put("data", list);
        return map;
    }


    @Override
    @CacheEvict(cacheNames = "order" ,allEntries = true)
    public Map<String, Object> orderInsert(Order order) {
        Map<String, Object> map = new HashMap<>();
        int count = orderMapper.orderInsert(order);
        if (count >= 1) {

            map.put("flag", true);
        }
        if (count < 1) {
            map.put("flag", false);
        }
        return map;
    }


    @Override
    @CacheEvict(cacheNames = "order" ,allEntries = true)
    public Map<String, Object> orderUpdate(Order order) {
        Map<String,Object> map=new HashMap<>();
        if(order.getOrderStatus()==3||order.getOrderStatus()==4){
            LocalTime localTime = LocalTime.now();
            LocalDate localDate = LocalDate.now();
            Date date = Date.from(localTime.atDate(localDate).atZone(ZoneId.systemDefault()).toInstant());
            order.setOrderCompletionTime(date);
        }
        Integer count=this.orderMapper.OrderUpdate(order);
        if(count >=1){
            map.put("flag",true);
            map.put("code",0);
            map.put("massage","更新成功");
        }else {
            map.put("flag",false);
            map.put("code",1);
            map.put("massage","更新失败");
        }
        return map;
    }

    @Override
    @CacheEvict(cacheNames = "order" ,allEntries = true)
    public Map<String, Object> orderDelete(Order order) {
        Map<String,Object> map=new HashMap<>();
        Integer count =this.orderMapper.OrderDelete(order);
        if(count >=1){
            map.put("flag",true);
            map.put("code",0);
            map.put("massage","删除成功");
        }else {
            map.put("flag",false);
            map.put("code",1);
            map.put("massage","删除失败");
        }
        return map;
    }
    public Map<String, Object> orderCompleteNumber() {
        Map<String, Object> map = new HashMap<>();
        System.out.println();
        List<Map<String, Object>> count = orderMapper.orderCompleteNumber();
        PageInfo<Map<String, Object>> pageInfoNumber = new PageInfo<>(count);
        map.put("count", pageInfoNumber.getTotal());
        return map;
    }
    public Map<String, Object> orderNoCompleteNumber() {
        Map<String, Object> map = new HashMap<>();
        System.out.println();
        List<Map<String, Object>> count = orderMapper.orderNoCompleteNumber();
        PageInfo<Map<String, Object>> pageInfoNumber = new PageInfo<>(count);
        map.put("count", pageInfoNumber.getTotal());
        return map;
    }
    public Map<String, Object> orderNumber() {
        Map<String, Object> map = new HashMap<>();
        System.out.println();
        List<Map<String, Object>> count_0 = orderMapper.orderNumber0();
        PageInfo<Map<String, Object>> pageInfoNumber0 = new PageInfo<>(count_0);
        List<Map<String, Object>> count_1 = orderMapper.orderNumber1();
        PageInfo<Map<String, Object>> pageInfoNumber1 = new PageInfo<>(count_1);
        List<Map<String, Object>> count_2 = orderMapper.orderNumber2();
        PageInfo<Map<String, Object>> pageInfoNumber2 = new PageInfo<>(count_2);
        List<Map<String, Object>> count_3 = orderMapper.orderNumber3();
        PageInfo<Map<String, Object>> pageInfoNumber3 = new PageInfo<>(count_3);
        List<Map<String, Object>> count_4 = orderMapper.orderNumber4();
        PageInfo<Map<String, Object>> pageInfoNumber4 = new PageInfo<>(count_4);
        List<Map<String, Object>> count_5 = orderMapper.orderNumber5();
        PageInfo<Map<String, Object>> pageInfoNumber5 = new PageInfo<>(count_5);
        List<Map<String, Object>> count_6 = orderMapper.orderNumber6();
        PageInfo<Map<String, Object>> pageInfoNumber6= new PageInfo<>(count_6);

        map.put("count0", pageInfoNumber0.getTotal());
        map.put("count1", pageInfoNumber1.getTotal());
        map.put("count2", pageInfoNumber2.getTotal());
        map.put("count3", pageInfoNumber3.getTotal());
        map.put("count4", pageInfoNumber4.getTotal());
        map.put("count5", pageInfoNumber5.getTotal());
        map.put("count6", pageInfoNumber6.getTotal());

        return map;
    }
}
//
//    public Map<String, Object> stockDelete(Stock stock) {
//        Map<String,Object>  map=new HashMap<>();
//        int count = stockMapper.stockDelete(stock);
//        if (count>=1){
//
//            map.put("flag",true);
//        }
//        if (count<1){
//            map.put("flag",false);
//        }
//        return  map;
//    }
//    @Override
//    public Map<String, Object> stockUpdate(Stock stock) {
//        Map<String,Object> map=new HashMap<>();
//        Integer count=this.stockMapper.stockUpdate(stock);
//        if (count >=1) {
//            map.put("flag",true);
//            map.put("code",0);
//            map.put("massage","更新成功");
//        }else {
//            map.put("flag",false);
//            map.put("code",1);
//            map.put("massage","更新失败");
//        }
//        return map;
//    }
//
//}
